package edu.gatech.ealf.magiclenseplaf;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.accessibility.Accessible;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.ComponentUI;

public class MLComboBoxUI extends ComboBoxUI implements MouseListener, ActionListener {

    
    
    private ZoomHelper zoomer = null;
    private boolean popupVisible = false;
    public MLComboBoxUI() {
        super();
    }

    public static ComponentUI createUI(JComponent c) {
        return new MLComboBoxUI();
    }

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        MagicLenseLookAndFeel.getInstance().registerComponentInstallation(this, c);
        zoomer = new ZoomHelper(c);
        c.addMouseListener(this);
        ((JComboBox)c).addActionListener(this);
    }

    public void paint(Graphics g, JComponent c) {
    }

    @Override
    public void uninstallUI(JComponent c) {
        super.uninstallUI(c);
        MagicLenseLookAndFeel.getInstance().registerComponentUninstallation(this, c);
        c.removeMouseListener(this);
        ((JComboBox)c).removeActionListener(this);
        if(zoomer != null){
            zoomer.destroy();
        }
        zoomer = null;
    }

    @Override
    public void update(Graphics g, JComponent c) {
    }

    public void mouseClicked(MouseEvent e) {
        if(e.getButton()== MouseEvent.BUTTON2){
            zoomer.toggleZoomable();
        }
    }


    public void mouseEntered(MouseEvent e) {
        //System.out.println("MLComboBoxUI.mouseEntered");
        zoomer.zoom();
    }

    public void mouseExited(MouseEvent e) {

        popupVisible = ((JComboBox)zoomer.curComponent).isPopupVisible();
        //System.out.println("JComboBox.mouseExited(): Popup Visible = " + popupVisible);
        if(!popupVisible){
            //System.out.println("JComboBox.mouseExited(): unzooming");
            boolean hasFocus = zoomer.curComponent.hasFocus();
            zoomer.unZoom();
            if(hasFocus){
                zoomer.curComponent.requestFocusInWindow();
            }
        }
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public boolean isFocusTraversable(JComboBox c) {
        return true;
    }

    @Override
    public boolean isPopupVisible(JComboBox c) {
        return false;
    }

    @Override
    public void setPopupVisible(JComboBox c, boolean v) {
    }

    public void actionPerformed(ActionEvent e) {
        popupVisible = ((JComboBox)zoomer.curComponent).isPopupVisible();
        
        //if(!popupVisible){
            zoomer.unZoom();
        //}
    }
    
    @Override
    public Accessible getAccessibleChild(JComponent c, int i) {
        return null;
    }
}

